package com.yhp.servlet;

import com.yhp.bean.Users;
import com.yhp.service.UsersService;
import com.yhp.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//对登录页面进行判断
@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //接受接受参数
        //处理乱码
        String userName = req.getParameter("UserName");
        String userPassword = req.getParameter("UserPassword");
        System.out.println("获取前端信息：username="+userName+"userpassword"+userPassword);
        //调取service
        Users users = new UsersServiceImpl().longin(userName,userPassword);
        //调取页面
        if (users==null){
         //弹窗方式提示用户登录失败
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href='login.jsp';alert('输入有误！！！');</script>");
        }else {
            req.getSession().setAttribute("u1", users);
            resp.sendRedirect("index.jsp");
        }
    }
}
